package cn.itcast.nems.studentrecord.web.controller.b;

import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.journal.dto.JournalTransferCreateBo;
import cn.itcast.nems.journal.dto.JournalTransferInfoBDto;
import cn.itcast.nems.journal.dto.JournalTransferInfoVDto;
import cn.itcast.nems.journal.dto.JournalTransferQuery;
import cn.itcast.nems.journal.service.JournalTransferService;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.studentrecord.bo.JournalClazzModel;
import cn.itcast.nems.studentrecord.bo.SearchClazzQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 转班
 */
@Slf4j
@RestController
@RequestMapping("/b/journal/transfer")
@RequiredArgsConstructor
public class JournalTransferController {

    private final JournalTransferService journalTransferService;

    /**
     * 转班凭单页面，搜索班级
     *
     * @param queryParam
     * @return
     */
    @PostMapping(
            value = {
                    "/clazzes",
                    "/delay/clazzes",
                    "/suspended/clazzes",
            }
    )
    public ResponseEntity<Page<Map>> searchClazzes(@RequestBody BasePageRequest<SearchClazzQuery> queryParam) {
        queryParam.convert(50);
        JournalClazzModel clazzModel = CustomBeanUtil.copyProperties(queryParam.getCondition(), new JournalClazzModel());
        PageQueryModel<JournalClazzModel> queryModel = new PageQueryModel<>(queryParam.getPageNumber(), queryParam.getPageSize(), clazzModel);
        return ResponseEntity.ok(journalTransferService.searchClazzes(queryModel));
    }

    /**
     * 转班凭单页面，分页
     *
     * @param pageRequest
     * @return
     */
    @PostMapping(
            value = {
                    "/{type}/page/list",
                    "/delay/{type}/page/list",
            }
    )
    public ResponseEntity page(
            @RequestBody BasePageRequest<JournalTransferQuery> pageRequest
    ) {
        return ResponseEntity.ok(journalTransferService.page(
                pageRequest.getCondition(),
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageRequest.getPageNumber(), pageRequest.getPageSize())
        ));
    }

    /**
     * 待定班-新建按钮
     *
     * @param bo
     * @return
     */
    @PostMapping("/delay/create")
    public ResponseEntity<String> create1(@RequestBody JournalTransferCreateBo bo) {
        return ResponseEntity.ok(journalTransferService.create(bo));
    }

    /**
     * 转班-新建按钮
     *
     * @param bo
     * @return
     */
    @PostMapping
    public ResponseEntity<String> create2(@RequestBody JournalTransferCreateBo bo) {
        return ResponseEntity.ok(journalTransferService.create(bo));
    }

    /**
     * 凭单详情
     *
     * @param id
     * @return
     */
    @GetMapping(
            value = {
                    "/{id}",
                    "/delay/{id}"
            }
    )
    public ResponseEntity<JournalTransferInfoVDto> info(
            @PathVariable String id
    ) {
        return ResponseEntity.ok(journalTransferService.info(id));
    }

    @PutMapping(
            value = {
                    "/{id}", // TODO 转班-新建跳转/详情-提交按钮
                    "/delay/{id}" // TODO 待定班-新建跳转/详情-提交按钮
            }
    )
    public ResponseEntity<Boolean> update(
            @PathVariable String id,
            @RequestBody JournalTransferInfoBDto info
    ) {
        info.setId(id);
        return ResponseEntity.ok(journalTransferService.update(info));
    }

    /**
     * 删除凭单
     *
     * @param id
     * @return
     */
    @DeleteMapping(
            value = {
                    "/{id}",
                    "/{type}/{id}"
            }
    )
    public ResponseEntity<Boolean> delete(
            @PathVariable String id
    ) {
        return ResponseEntity.ok(journalTransferService.delete(id));
    }

    /**
     * 红点（废弃via公共方法）
     */
    @Deprecated
    @GetMapping("/my_approving/flag")
    public ResponseEntity<Boolean> isHaveWaitApprovingList() {
        return ResponseEntity.ok().body(journalTransferService.isHaveWaitApprovingList());
    }

}
